package com.guo.rabbitmq.quickstart;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.concurrent.TimeoutException;

/**
 * @author guo
 * @date 2021/11/17 10:04 星期三
 **/
public class RabbitMqProducer {

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {

        //创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();

        connectionFactory.setHost("192.168.80.101");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //获取连接
        Connection connection = connectionFactory.newConnection();

        //创建channel
        Channel channel = connection.createChannel();
        System.out.println(LocalDateTime.now()+"   producer获取channel成功");
        for (int i = 0; i < 100; i++) {

            Thread.sleep(500);
            String msg = LocalDateTime.now() +  "   第一次发送消息"+ i;
            channel.basicPublish("","test001",null,msg.getBytes());
        }

        channel.close();
        connection.close();

    }
}
